package com.xes.attend.scheduler.impl;


import com.xes.attend.config.ServerConfig;
import com.xes.attend.domain.AttendResult;
import com.xes.attend.domain.BaseException;
import com.xes.attend.scheduler.Scheduler;
import com.xes.attend.service.AttendDataService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;

/***
 * @description 上传已考勤数据调度任务
 * @author wjl
 * @ClassName: UploadAttendDataByTodaySchedulerService
 * @Version 版本
 * @ModifiedBy wjl
 * @Copyright xes
 * @date 2015年11月27日 上午11:41:13
 */
@Service
public class UploadAttendDataByTodaySchedulerService implements Scheduler {
    private static final Logger log = LogManager.getLogger(UploadAttendDataByTodaySchedulerService.class);
    @Autowired
    private ServerConfig config;
    @Autowired
    private AttendDataService attendDataService;

    @Override
    @Scheduled(fixedDelay = 1000 * 60 * 5)        // 5 minuts
    public void execute() {
        // 未完成预考勤下载，暂时不上传数据
        if (!config.getDownloadAttendByToday()) {
            return;
        }
        StopWatch stopWatch = new StopWatch("上传已考勤数据调度任务VenueId：" + config.getVenueId());
        stopWatch.start("上传");
        try {
            AttendResult result = null;
            try {
                result = attendDataService.uploadAttendDataByToday(config.getVenueId());
            } catch (BaseException e) {
                log.error("上传已考勤数据失败:{}:{},{}", e.getResultCode(), e.getResultCode(), e.getMessage());
            }
            log.info("上传已考勤数据：{}", result.getDataCount());
        } catch (Exception e) {
            e.printStackTrace();
        }
        stopWatch.stop();
		log.debug(stopWatch.prettyPrint());
    }

}
